<?php
/**
 * 产品模板
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;

use App\Services\BrandService\BrandToolService;
use App\Services\ColourService\BackstageColourService;
use App\Services\ColourService\ColourTool;
use App\Services\CrowdService\CrowdService;
use App\Services\CrowdService\CrowdTool;
use App\Services\ImgServices\ImgTool;
use App\Services\ProdTempCategoryService\ProdTempCategoryBackService;
use App\Services\ProdTempCategoryService\ProdTempCategoryTool;
use App\Services\ProductTempService\ProductTempAttributeService;
use App\Services\ProductTempService\ProductTempService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\SizeService\BackstageSizeService;
use App\Services\TagService\TagService;
use App\Services\TagService\TagTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use App\Services\ModifyResourcePriceRecordsService\ModifyResourcePriceRecordsService;
class ProductTempController extends Controller
{
    protected $productTempSer;
    protected $productTempTool;
    protected $categoryBackSer;
    protected $sizeService;
    protected $crowdService;
    protected $colourService;
    protected $tagSer;
    protected $modifyResourcePriceRecordsSer;
    /**
     * ProductTempController constructor.
     */
    public function __construct(ProductTempService $productTempSer,
                                ProductTempTool $productTempTool,
                                ProdTempCategoryBackService $categoryBackSer,
                                CrowdService $crowdService,
                                ModifyResourcePriceRecordsService $modifyResourcePriceRecordsService,
                                TagService $tagSer,
                                BackstageSizeService $sizeService,
                                BackstageColourService $colourService
    )
    {
        $this->productTempSer=$productTempSer;
        $this->productTempTool=$productTempTool;
        $this->categoryBackSer=$categoryBackSer;
        $this->sizeService=$sizeService;
        $this->crowdService=$crowdService;
        $this->colourService=$colourService;
        $this->tagSer=$tagSer;
        $this->modifyResourcePriceRecordsSer=$modifyResourcePriceRecordsService;
    }

    /**
     * 添加产品模板页面
     * @param Request $request
     * @return $this
     */
    public function getAddProdTemp(Request $request)
    {
        $categorys=$this->categoryBackSer->getAllProdTempCategoryList(false);
        return view('backstage.productTemp.addProdTemp')->with('categorys',$categorys);
    }

    /**
     * 获取产品模板列表
     * @param Request $request
     * @return $this|void
     */
    public function getProdTempList(Request $request)
    {
        $page=intval($request->input('page'));
        $brand_id=trim($request->input('brand_id'));
        $temp_category_id=intval($request->input('category_id',0));
        if(!empty($page)&&(!is_int($page)||($page>100))){
            return redirect()->back()->with('error_message','暂无数据');
        }
        $search=trim($request->input('search',''));
        $product_temps=$this->productTempSer->searchProdTempList($search,$brand_id,$temp_category_id);
        if($product_temps===false){
            return redirect('/backstage/product-temp/product-temp-list')->with('error_message','暂无数据');
        }
        foreach ($product_temps as $product_temp){
            $tag_ids=TagTool::getTagIdsByResourceId($product_temp->product_temp_id,2,1);
            $product_temp->tag_ids=$tag_ids;
            $product_temp->category_name=ProdTempCategoryTool::productTempCategoryNameByProdTempCategoryId($product_temp->product_temp_category_id);
            $tag_names='';
            foreach ($tag_ids as $tag_id){
                $tag_name=TagTool::getTagNameByTagId($tag_id,1);
                if(!empty($tag_name)){
                    if(!empty($tag_names)){
                        $tag_names.= '|';
                    }
                    $tag_names.= $tag_name;
                }
            }
            $product_temp->tag_names=empty($tag_names)?'':$tag_names;
        }
        $brands=BrandToolService::getBrandsWhereNotInBrandIds(array());
        $product_temp_categorys=ProdTempCategoryTool::getProdTempCategoryList();
        return view('backstage.productTemp.productTempList')
            ->with('product_temps',$product_temps)
            ->with('brands',$brands)
            ->with('brand_id',$brand_id)
            ->with('temp_category_id',$temp_category_id)
            ->with('temp_categorys',$product_temp_categorys);
    }

    /**
     * 产品模板更新页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getUpdateProdTemp(Request $request)
    {
        $id=trim($request->input('id'));
        $product_temp=$this->productTempTool->getProdTempByProdTempId($id);
        if(empty($product_temp)){
            return redirect()->back()->with('error_message','产品模板不存在');
        }
        $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        $product_temp_crowds=$this->crowdService->getProductCrowdByProductTempId($id);
        $product_temp_colours=$this->colourService->getProductColoursByProductTempId($id);
        foreach ($product_temp_colours as $product_temp_colour){
            $product_temp_colour->colour_name=ColourTool::getColourNameByColourId($product_temp_colour->colour_id);
        }
        $product_temp_sizes=$this->sizeService->getProductSizesByProductTempId($id);
        $categorys=$this->categoryBackSer->getAllProdTempCategoryList();
        $sizes=$this->sizeService->getSizeByProdClassId($product_temp->product_temp_category_id);
        $temp_attributes=ProductTempAttributeService::getTempAttributesByClassId($product_temp->product_temp_category_id);
        //已选择产品模板属性
        $chosen_temp_attributes=ProductTempAttributeService::getTempAttrsByProductTempId($product_temp->product_temp_id);
        $product_temp_crowds=empty($product_temp_crowds)?array():$product_temp_crowds;
        $product_temp_colours=empty($product_temp_colours)?array():$product_temp_colours;
        $product_temp_sizes=empty($product_temp_sizes)?array():$product_temp_sizes;
        $categorys=empty($categorys)?array():$categorys;
        $sizes=empty($sizes)?array():$sizes;
        $temp_attributes=empty($temp_attributes)?array():$temp_attributes;
        $chosen_temp_attributes=empty($chosen_temp_attributes)?array():$chosen_temp_attributes;
        $chosen_temp_attributes_format=array();
        foreach ($chosen_temp_attributes as $chosen_temp_attribute){
            $attr_id=$chosen_temp_attribute->attr_id;
            $chosen_temp_attributes_format[$attr_id]=$chosen_temp_attribute;
        }
        return view('backstage.productTemp.prodTempUpdate')
            ->with('temp_crowds',$product_temp_crowds)
            ->with('temp_colours',$product_temp_colours)
            ->with('temp_sizes',$product_temp_sizes)
            ->with('categorys',$categorys)
            ->with('sizes',$sizes)
            ->with('temp_attributes',$temp_attributes)
            ->with('chosen_temp_attributes',$chosen_temp_attributes_format)
            ->with('product_temp',$product_temp);
    }
    /**
     * 添加产品模板
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAddProdTemp(Request $request)
    {
        $rule=array(
            'name'=>'required|string|',
            'brand_id'=>'required|string|',
            'postage'=>'|numeric|min:0',
            'category_id'=>'required|integer|min:1',
            'default_price'=>'required|numeric|min:0',
            'default_stock'=>'required|integer|min:1',
        );
        $name=trim($request->input('name'));
        $brand_id=trim($request->input('brand_id'));
        $postage=floatval($request->input('postage'));
        $category_id=intval($request->input('category_id'));
        $default_price=floatval($request->input('default_price'));
        $default_stock=intval($request->input('default_stock'));
        $has_size=intval($request->input('has_size'));
        $crowd_ids=$request->input('crowd_ids');
        $colour_ids=$request->input('colour_ids');
        $sizes=$request->input('sizes');
        $attributes=$request->input('attributes');
        $validator=ProductTempTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('name')){
                $result['state']=-9;
                $result['message']='模板名格式错误';
                return response()->json($result);
            }
            if($validator->errors()->first('brand_id')){
                $result['state']=-10;
                $result['message']='请选择品牌';
                return response()->json($result);
            }
            if($validator->errors()->first('postage')){
                $result['state']=-11;
                $result['message']='包邮格式错误';
                return response()->json($result);
            }
            if($validator->errors()->first('category_id')){
                $result['state']=-12;
                $result['message']='模板分类格式错误';
                return response()->json($result);
            }
            if($validator->errors()->first('default_price')){
                $result['state']=-13;
                $result['message']='默认单价格式错误';
                return response()->json($result);
            }
            if($validator->errors()->first('default_stock')){
                $result['state']=-14;
                $result['message']='默认库存格式错误';
                return response()->json($result);
            }
        }
        if(empty($default_price)){
            $result['state']=-15;
            $result['message']='模板单价为零，天下没有免费的午餐~~~';
            return response()->json($result);
        }
        $attributes=empty($attributes)?array():$attributes;
        $sizes=empty($sizes)?array():$sizes;
        if(!is_array($crowd_ids)){
            $result['state']=-2;
            $result['message']='人群格式错误';
            return response()->json($result);
        }
        if(!is_array($colour_ids)){
            $result['state']=-3;
            $result['message']='颜色格式错误';
            return response()->json($result);
        }
        if(!empty($sizes)&&!is_array($sizes)){
            $result['state']=-4;
            $result['message']='尺码格式错误';
            return response()->json($result);
        }
        if(!empty($attribute)&&!is_array($attributes)){
            $result['state']=-5;
            $result['message']='模板属性格式错误';
            return response()->json($result);
        }
        $brand_exist=BrandToolService::brandExistByBrandId($brand_id);
        if(!$brand_exist){
            $result['state']=-6;
            $result['message']='品牌不能使用';
            return response()->json($result);
        }
        foreach ($crowd_ids as $crowd_id){
            $crowd_exist=CrowdTool::crowdExistByCrowdId($crowd_id);
           if(!$crowd_exist){
               $result['state']=-7;
               $result['message']='人群id为：'.$crowd_id.'，不能用';
               return response()->json($result);
           }
        }
        foreach ($colour_ids as $colour_id){
            $colour_exist=ColourTool::colourExistByColourId($colour_id);
            if(!$colour_exist){
                $result['state']=-8;
                $result['message']='颜色Id为：'.$colour_id.'，不能用';
                return response()->json($result);
            }
        }
        foreach ($sizes as $size){

        }
        foreach ($attributes as $attribute){

        }
        $data['name']=empty($name)?'':$name;
        $data['brand_id']=empty($brand_id)?'':$brand_id;
        $data['postage']=empty($postage)?0:$postage;
        $data['product_temp_category_id']=empty($category_id)?0:$category_id;
        $data['colour_ids']=empty($colour_ids)?array():$colour_ids;
        $data['crowd_ids']=empty($crowd_ids)?array():$crowd_ids;
        $data['sizes']=empty($sizes)?array():$sizes;
        $data['attributes']=empty($attributes)?array():$attributes;
        $data['has_size']=empty($has_size)?0:$has_size;
        $data['default_price']=empty($default_price)?0:$default_price;
        $data['default_stock']=empty($default_stock)?0:$default_stock;
        $res=$this->productTempSer->addProdTemp($data);
        if($res){
            $result['state']=1;
            $result['message']='保存成功';
        }else{
            $result['state']=-1;
            $result['message']='保存失败';
        }
        return response()->json($result);
    }

    /**
     * 更新产品模板主要信息
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function updateProdTemp(Request $request)
    {
        $rule=array(
            'name'=>'required|string|',
            'brand_id'=>'required|string|',
            'postage'=>'|numeric|min:0',
            'category_id'=>'required|integer|min:1',
            'default_price'=>'required|numeric|min:0',
            'default_stock'=>'required|integer|min:1',
            'bluk_update_stock'=>'required|numeric|min:0',
        );
        $name=trim($request->input('name'));
        $brand_id=trim($request->input('brand_id'));
        $default_price=floatval($request->input('default_price'));
        $postage=floatval($request->input('postage'));
        $category_id=intval($request->input('category_id'));
        $default_stock=intval($request->input('default_stock'));
        $bluk_update_stock=intval($request->input('bluk_update_stock'));
        $has_size=intval($request->input('has_size'));
        $crowd_ids=$request->input('crowd_ids');
        $colour_ids=$request->input('colour_ids');
        $sizes=$request->input('sizes');
        $attributes=$request->input('attributes');
        $product_temp_id=trim($request->input('product_temp_id'));
        $validator=ProductTempTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('name')){
                $result['state']=-9;
                $result['message']='模板名格式错误';
                return response()->json($result);
            }else if($validator->errors()->first('brand_id')){
                $result['state']=-10;
                $result['message']='请选择品牌';
                return response()->json($result);
            }else if($validator->errors()->first('postage')){
                $result['state']=-11;
                $result['message']='包邮格式错误';
                return response()->json($result);
            }else if($validator->errors()->first('category_id')){
                $result['state']=-12;
                $result['message']='模板分类格式错误';
                return response()->json($result);
            }else if($validator->errors()->first('default_stock')){
                $result['state']=-14;
                $result['message']='默认库存格式错误';
                return response()->json($result);
            }else if($validator->errors()->first('default_price')){
                $result['state']=-16;
                $result['message']='默认单价格式错误';
                return response()->json($result);
            }else if($validator->errors()->first('bluk_update_stock')&&($bluk_update_stock==1)){
                $result['state']=-15;
                $result['message']='批量修改单价格式错误';
                return response()->json($result);
            }
        }
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            $result['state']=-16;
            $result['message']='模板不存在';
            return response()->json($result);
        }
        $attributes=empty($attributes)?array():$attributes;
        $sizes=empty($sizes)?array():$sizes;
        if(!is_array($crowd_ids)){
            $result['state']=-2;
            $result['message']='人群格式错误';
            return response()->json($result);
        }
        if(!is_array($colour_ids)){
            $result['state']=-3;
            $result['message']='颜色格式错误';
            return response()->json($result);
        }
        if(!empty($sizes)&&!is_array($sizes)){
            $result['state']=-4;
            $result['message']='尺码格式错误';
            return response()->json($result);
        }
        if(!empty($attribute)&&!is_array($attributes)){
            $result['state']=-5;
            $result['message']='模板属性格式错误';
            return response()->json($result);
        }
        $brand_exist=BrandToolService::brandExistByBrandId($brand_id);
        if(!$brand_exist){
            $result['state']=-6;
            $result['message']='品牌不能使用';
            return response()->json($result);
        }
        foreach ($crowd_ids as $crowd_id){
            $crowd_exist=CrowdTool::crowdExistByCrowdId($crowd_id);
            if(!$crowd_exist){
                $result['state']=-7;
                $result['message']='人群id为：'.$crowd_id.'，不能用';
                return response()->json($result);
            }
        }
        foreach ($colour_ids as $colour_id){
            $colour_exist=ColourTool::colourExistByColourId($colour_id);
            if(!$colour_exist){
                $result['state']=-8;
                $result['message']='颜色Id为：'.$colour_id.'，不能用';
                return response()->json($result);
            }
        }
        foreach ($sizes as $size){

        }
        foreach ($attributes as $attribute){

        }
        $data['name']=empty($name)?'':$name;
        $data['brand_id']=empty($brand_id)?'':$brand_id;
        $data['postage']=empty($postage)?0:$postage;
        $data['product_temp_category_id']=empty($category_id)?0:$category_id;
        $data['colour_ids']=empty($colour_ids)?array():$colour_ids;
        $data['crowd_ids']=empty($crowd_ids)?array():$crowd_ids;
        $data['sizes']=empty($sizes)?array():$sizes;
        $data['attributes']=empty($attributes)?array():$attributes;
        $data['has_size']=empty($has_size)?0:$has_size;
        $data['bluk_update_stock']=empty($bluk_update_stock)?0:$bluk_update_stock;
        $data['default_price']=empty($default_price)?0:$default_price;
        $data['product_temp_id']=$product_temp_id;
        if($bluk_update_stock===1){
            $data['default_stock']=empty($default_stock)?0:$default_stock;
        }
        $res=$this->productTempSer->updateProdTemp($data);
        if($res){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    /**
     * 搜索产品模板
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function searchTemp(Request $request){
        $prod_temps =  $this->productTempSer->searchAllProdTempList($request);
        $result=array('data'=>$prod_temps,'state'=>1,'message'=>'获取成功');
        return response()->json($result);
    }

    /***
     * sku获取产品模板库存信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTempStockBySku(Request $request){
        $sku=trim($request->input('sku'));
        if(empty($sku)){
            $result['state']=-2;
            $result['message']='sku为空';
            return response()->json($result);
        }
        $product_temp_stock =  $this->productTempTool->getProductTempStockBySku($sku,1);
        if(empty($product_temp_stock)){
            $result['state']=-1;
            $result['message']='信息不存在';
        }else{
            $result['state']=1;
            $result['message']='成功';
            $result['temp_stock']=$product_temp_stock;
        }
        return response()->json($result);
    }
    /**
     * 某一产品分类下的属性列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function tempAttrList(Request $request){
        $product_category_id =  intval($request->input('temp_category_id'));
        $product_category_exist = $this->categoryBackSer->prodCategoryExistById($product_category_id);
        if(!$product_category_exist){
            $result['state']=-2;
            $result['data']=array();
            $result['message']='找不到对应模板分类';
        }
        $sizes=$this->sizeService->getSizeByProdClassId($product_category_id);
        $temp_attributes=ProductTempAttributeService::getTempAttributesByClassId($product_category_id);
        $data['attributes']=empty($temp_attributes)?array():$temp_attributes;
        $data['sizes']=empty($sizes)?array():$sizes;
        $result['data']=$data;
        $result['state']=1;
        $result['message']='成功！';
        return response()->json($result);
    }

    /**
     * 产品模板删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delProdTemp(Request $request){
        $product_temp_id=trim($request->input('id'));
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            $result['state']=-2;
            $result['message']='模板不存在';
            return response()->json($result);
        }
        $res=$this->productTempSer->delProdTempByProdTempId($product_temp_id);
        if($res){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }

    /**
     * 添加库存
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getAddStockPage(Request $request){
        $product_temp_id=trim($request->input('id'));
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        $product_temp_colours=$this->productTempSer->getProductTempColours($product_temp_id);
        foreach ($product_temp_colours as $temp_colour){
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($temp_colour->product_temp_id,$temp_colour->colour_id,2);
            $has_modify_price=false;
            $modify_price=0;
            if ($is_resource_modify_price!==false){
                $has_modify_price=true;
                $modify_price=$is_resource_modify_price['modify_price'];
            }
            $temp_colour->has_modify_price=$has_modify_price;
            $temp_colour->modify_price=$modify_price;
        }
        return view('backstage.productTemp.productTempAddStock')
            ->with('product_temp',$product_temp)
            ->with('temp_colours',$product_temp_colours);
    }

    /**
     * 获取尺码列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function productTempSizeList(Request $request){
        $product_temp_id=trim($request->input('id'));
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
            $product_temp->temp_category_name=ProdTempCategoryTool::productTempCategoryNameByProdTempCategoryId($product_temp->product_temp_category_id);
        }else{
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $temp_sizes=$this->productTempSer->getProductTempSize($product_temp_id);
        $temp_size_format=$this->productTempSer->getProductTempSizeFormat($product_temp_id);
        if(!empty($temp_size_format)){
            $temp_size_format->measure_method_image=ImgTool::getImgUrlByImgPath($temp_size_format->measure_method_image);
        }
        return view('backstage.productTemp.productTempSizeList')
            ->with('product_temp',$product_temp)
            ->with('temp_size_format',$temp_size_format)
            ->with('temp_sizes',$temp_sizes);
    }

    /**
     * 更新尺码格式
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateProductTempSizeFormatPage(Request $request){
        $product_temp_id=trim($request->input('id'));
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        $temp_size_format=$this->productTempSer->getProductTempSizeFormat($product_temp_id);
        if(!empty($temp_size_format)){
            $temp_size_format->measure_method_image=ImgTool::getImgUrlByImgPath($temp_size_format->measure_method_image);
        }
        return view('backstage.productTemp.addProductTempSizeFormat')
            ->with('product_temp',$product_temp)
            ->with('temp_size_format',$temp_size_format);
    }

    /**
     * 更新产品模板尺码格式
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateProductTempSizeFormat(Request $request){
        $rule=array(
            'product_temp_id'=>'required|string|',
            'size_list'=>'|string|',
            'measure_method_image'=>'image',
        );
        $product_temp_id=trim($request->input('product_temp_id'));
        $size_list=trim($request->input('size_list'),'');
        $image=$request->file('measure_method_image');
        $val_data['product_temp_id']=$product_temp_id;
        $val_data['size_list']=$size_list;
        $val_data['measure_method_image']=$image;
        $validator=ProductTempTool::store($request,$rule);
        $validator->after(function($validator) use ($val_data)
        {
            $size_list=$val_data['size_list'];
            if(!empty($size_list)){
                $size_arr=explode('|',$size_list);
                if(sizeof($size_arr)<4){
                    $validator->errors()->add('size_list', '格式不正确');
                }
            }
            $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($val_data['product_temp_id']);
            if(!$product_temp_exist){
                $validator->errors()->add('product_temp_id', '模板不存在');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }
        $res=$this->productTempSer->updateProductTempSizeFormat($val_data);
        if($res){
            return redirect()->back()->with('success_message', '保存成功');
        }
        return redirect()->back()->with('error_message', '保存失败');
    }
    /**
     * 创作区域列表页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function productTempCustomZoneListPage(Request $request){
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        $product_temp_colour_exist=$this->productTempSer->productTempColourExist($product_temp_id,$colour_id,1);
        if(!$product_temp_colour_exist){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        $data=$this->productTempSer->getProductTempCustomZoneList($product_temp_id,$colour_id);
        if($data===false){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        $product_temp_colour=$data['temp_colour'];
        if(!empty($product_temp_colour)){
            $product_temp_colour->colour_name=ColourTool::getColourNameByColourId($product_temp_colour->colour_id);
        }
        $temp_custom_zones=$data['temp_custom_zones'];
        $temp_custom_zones=empty($temp_custom_zones)?array():$temp_custom_zones;
        foreach ($temp_custom_zones as $temp_custom_zone){
            if(!empty($temp_custom_zone->original_image)){
                $temp_custom_zone->original_image=ImgTool::getImgUrlByImgPath($temp_custom_zone->original_image);
            }
            if(!empty($temp_custom_zone->thumbnail)){
                $temp_custom_zone->thumbnail=ImgTool::getImgUrlByImgPath($temp_custom_zone->thumbnail);
            }
        }
        return view('backstage.productTemp.productTempCustomZoneList')
            ->with('temp_colour',$product_temp_colour)
            ->with('temp_custom_zones',$temp_custom_zones)
            ->with('product_temp',$product_temp);
    }

    /**
     * 产品模板定制区域工艺列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function craftsmanshipList(Request $request){
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        $custom_zone_id=intval($request->input('custom_zone_id'));
        if(empty($custom_zone_id)){
            return redirect()->back()->with('error_message', '定制区域编号参数错误');
        }
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        $product_temp_colour=$this->productTempSer->getProductTempColourByProductTempIdAndColour($product_temp_id,$colour_id,1);
        if(empty($product_temp_colour)){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        if(!empty($product_temp_colour)){
            $product_temp_colour->colour_name=ColourTool::getColourNameByColourId($product_temp_colour->colour_id);
        }
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        $temp_craftsmanships=$this->productTempSer->getProductTempCraftsmanshipListByCustomZoneId($custom_zone_id);
        return view('backstage.productTemp.productTempCraftsmanshipList')
            ->with('temp_colour',$product_temp_colour)
            ->with('temp_custom_zone',$temp_custom_zone)
            ->with('temp_craftsmanships',$temp_craftsmanships)
            ->with('product_temp',$product_temp);
    }

    /***
     * 制作工艺对应的颜色列表
     * @param Request $request
     * @return $this
     */
    public function craftsmanshipColourList(Request $request){
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $temp_craftsmanship_id=intval($request->input('craftsmanship_id'));
        if(empty($custom_zone_id)||empty($temp_craftsmanship_id)){
            return redirect()->back()->with('error_message', '参数有错');
        }
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $temp_craftsmanship=ProductTempTool::getCraftsmanshipById($temp_craftsmanship_id,1);
        if(empty($temp_craftsmanship)){
            return redirect()->back()->with('error_message', '对应工艺数据');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        $product_temp_colour=$this->productTempSer->getProductTempColourByProductTempIdAndColour($product_temp_id,$colour_id,1);
        if(empty($product_temp_colour)){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        if(!empty($product_temp_colour)){
            $product_temp_colour->colour_name=ColourTool::getColourNameByColourId($product_temp_colour->colour_id);
        }
        $temp_craftsmanship_colours=$this->productTempSer->getProductTempCraftsmanshipColours($temp_craftsmanship_id);
        foreach ($temp_craftsmanship_colours as $temp_craftsmanship_colour){
            $colour=ColourTool::getColourSimpleInfoByColourId($temp_craftsmanship_colour->colour_id);
            $temp_craftsmanship_colour->colour_name='';
            $temp_craftsmanship_colour->colour_code='';
            $temp_craftsmanship_colour->colour_image='';
            if(!empty($colour)){
                $temp_craftsmanship_colour->colour_name=$colour->colour_name;
                $temp_craftsmanship_colour->colour_code=$colour->colour_code;
                $temp_craftsmanship_colour->colour_image=$colour->colour_image;
            }
        }
        $sel_colour_ids=ProductTempTool::getCraftsmanshipColourIdsByCraftsmanshipId($temp_craftsmanship_id);
        $un_sel_colours=ColourTool::getColoursWhereNotInColourIds($sel_colour_ids);
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        $temp_craftsmanship=ProductTempTool::getCraftsmanshipById($temp_craftsmanship_id,1);
        return view('backstage.productTemp.productTempCraftsmanshipColourList')
            ->with('temp_colour',$product_temp_colour)
            ->with('temp_custom_zone',$temp_custom_zone)
            ->with('un_sel_colours',$un_sel_colours)
            ->with('temp_craftsmanship_id',$temp_craftsmanship_id)
            ->with('temp_craftsmanship',$temp_craftsmanship)
            ->with('temp_craftsmanship_colours',$temp_craftsmanship_colours)
            ->with('product_temp',$product_temp);
    }

    /***
     * 获取定制区域默认颜色列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function craftsmanshipDefaultColourList(Request $request){
        $rule=array(
            'page'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$rule);
        $page=intval($request->input('page'));
        $redirect_url='/backstage/product-temp/product-temp-tag-list';//换成第一页的URL
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    return redirect($redirect_url)->with('error_message', '获取页数有问题!');
                }
            }
        }
        if($page>100000){
            return redirect($redirect_url)->with('error_message', '获取页数有问题!');
        }
        $default_colours=$this->productTempSer->getCraftsmanshipDefaultColourList($request);
        $sel_colour_ids=$this->productTempTool->getAllCraftsmanshipDefaultColourIds(1);
        $un_sel_colours=ColourTool::getColoursWhereNotInColourIds($sel_colour_ids);
        return view('backstage.productTemp.craftsmanshipDefaultColours')
            ->with('default_colours',$default_colours)
            ->with('un_sel_colours',$un_sel_colours);
    }
    /**
     * 创作区域页面信息更新
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateCustomZonePage(Request $request){
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        $custom_zone_id=intval($request->input('custom_zone_id'));
        if(empty($product_temp_id)||empty($colour_id)||empty($custom_zone_id)){
            return redirect()->back()->with('error_message', '请求参数错误');
        }
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($custom_zone)){
            return redirect()->back()->with('error_message', '找不到设计区域信息');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        $product_temp_colour_exist=$this->productTempSer->productTempColourExist($product_temp_id,$colour_id,1);
        if(!$product_temp_colour_exist){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        $product_temp_colour=$this->productTempSer->getProductTempColourByProdTempIdAndColourId($product_temp_id,$colour_id);
        if(!empty($product_temp_colour)){
            $product_temp_colour->colour_name=ColourTool::getColourNameByColourId($colour_id);
        }
        $temp_custom_zone_positions=$this->productTempSer->getProductTempCustomZonePositions($product_temp->product_temp_category_id);
        return view('backstage.productTemp.updateProductTempCustomZone')
            ->with('temp_colour',$product_temp_colour)
            ->with('custom_zone',$custom_zone)
            ->with('custom_zone_positions',$temp_custom_zone_positions)
            ->with('product_temp',$product_temp);
    }

    /**
     * 添加工艺页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function addCraftsmanshipPage(Request $request){
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $temp_custom_zone_id=intval($request->input('temp_custom_zone_id'));
        if(empty($custom_zone_id)){
            return redirect()->back()->with('error_message', '请求参数错误');
        }
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($temp_custom_zone)){
            return redirect()->back()->with('error_message', '设计区域不存在');
        }
        return view('backstage.productTemp.addCraftsmanship');
    }

    /**
     * 更新工艺页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateCraftsmanshipPage(Request $request){
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        $craftsmanship_type=intval($request->input('colour_id'));
        if(empty($custom_zone_id)||empty($craftsmanship_type)){
            return redirect()->back()->with('error_message', '请求参数错误');
        }
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($temp_custom_zone)){
            return redirect()->back()->with('error_message', '设计区域不存在');
        }
        $temp_craftsmanship=ProductTempTool::getCraftsmanshipByCraftsmanshipTypeAndZoneId($craftsmanship_type,$custom_zone_id,1);
        if(empty($temp_craftsmanship)){
            return redirect()->back()->with('error_message', '当前工艺不存在');
        }
        return view('backstage.productTemp.updateCraftsmanship')
            ->with('temp_craftsmanship',$temp_craftsmanship);
    }

    /**
     * 更新制作工艺
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function updateCraftsmanship(Request $request){
        $craftsmanship_type=intval($request->input('craftsmanship_type'));
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $colour_id=intval($request->input('colour_id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        if(empty($custom_zone_id)||empty($craftsmanship_type)){
            return redirect()->back()->with('error_message', '请求参数错误');
        }
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($temp_custom_zone)){
            return redirect()->back()->with('error_message', '设计区域不存在');
        }
        $data['craftsmanship_type']=$craftsmanship_type;
        $data['craftsmanship_name']=ProductTempTool::getCraftsmanshipNameByCraftsmanshipId($craftsmanship_type);
        $data['temp_custom_zone_id']=$custom_zone_id;
        $res=$this->productTempSer->saveCraftsmanship($data);
        if($res){
            $redirect_url='/backstage/product-temp/craftsmanship-list?custom_zone_id='.$custom_zone_id.'&product_temp_id='.$product_temp_id.'&colour_id='.$colour_id;
            return redirect($redirect_url)->with('success_message', '保存成功');
        }
        return redirect()->back()->with('error_message', '保存失败');
    }

    /***
     * 更新制作区域下的工艺
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCraftsmanship(Request $request){
        $caftsmanship_type=intval($request->input('caftsmanship_type'));
        $custom_zone_id=intval($request->input('temp_custom_zone_id'));
        if(empty($caftsmanship_type)||empty($custom_zone_id)||!is_int($caftsmanship_type)||!is_int($custom_zone_id)){
            $result['state']=-2;
            $result['message']='参数格式不正确';
            return response()->json($result);
        }
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($temp_custom_zone)){
            $result['state']=-3;
            $result['message']='设计区域不存在';
            return response()->json($result);
        }
        $data['craftsmanship_type']=$caftsmanship_type;
        $data['craftsmanship_name']=ProductTempTool::getCraftsmanshipNameByCraftsmanshipId($caftsmanship_type);
        $data['temp_custom_zone_id']=$custom_zone_id;
        $res=$this->productTempSer->saveCraftsmanship($data);
        if($res){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     *  添加制作工艺下可使用颜色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCraftsmanshipColour(Request $request){
    $product_temp_craftsmanship_id=intval($request->input('product_temp_craftsmanship_id'));
    $colour_id=intval($request->input('colour_id'));
    if(empty($product_temp_craftsmanship_id)||empty($colour_id)||!is_int($product_temp_craftsmanship_id)||!is_int($colour_id)){
        $result['state']=-2;
        $result['message']='参数格式不正确';
        return response()->json($result);
    }
    $colour_exist=ColourTool::colourExistByColourId($colour_id);
    if(!$colour_exist){
       $result['state']=-3;
       $result['message']='该颜色不存在';
       return response()->json($result);
    }
    $product_temp_craftsmanship_exist=ProductTempTool::productTempCraftsmanshipExist($product_temp_craftsmanship_id);
    if(!$product_temp_craftsmanship_exist){
        $result['state']=-3;
        $result['message']='该工艺不存在';
        return response()->json($result);
    }
    $data['product_temp_craftsmanship_id']=$product_temp_craftsmanship_id;
    $data['colour_id']=$colour_id;
    $res=$this->productTempSer->addCraftsmanshipColour($data);
    if($res){
        $result['state']=1;
        $result['message']='添加成功';
    }else{
        $result['state']=-1;
        $result['message']='添加失败';
    }
    return response()->json($result);
}
    /**
     * 保存制作工艺
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveCraftsmanship(Request $request){
        $craftsmanship_type=intval($request->input('craftsmanship_type'));
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $colour_id=intval($request->input('colour_id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        if(empty($custom_zone_id)||empty($craftsmanship_type)){
            return redirect()->back()->with('error_message', '请求参数错误');
        }
        $temp_custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($temp_custom_zone)){
            return redirect()->back()->with('error_message', '设计区域不存在');
        }
        $data['craftsmanship_type']=$craftsmanship_type;
        $data['craftsmanship_name']=ProductTempTool::getCraftsmanshipNameByCraftsmanshipId($craftsmanship_type);
        $data['temp_custom_zone_id']=$custom_zone_id;
        $res=$this->productTempSer->saveCraftsmanship($data);
        if($res){
            $redirect_url='/backstage/product-temp/craftsmanship-list?custom_zone_id='.$custom_zone_id.'&product_temp_id='.$product_temp_id.'&colour_id='.$colour_id;
            return redirect($redirect_url)->with('success_message', '保存成功');
        }
        return redirect()->back()->with('error_message', '保存失败');
    }
    /**
     * 前往添加创作区域页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function addCustomZonePage(Request $request){
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_id=intval($request->input('colour_id'));
        $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            return redirect()->back()->with('error_message', '找不到对应模板');
        }
        $product_temp=$this->productTempTool->getProdTempByProdTempId($product_temp_id);
        if(!empty($product_temp)){
            $product_temp->brand_name=BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        $product_temp_colour_exist=$this->productTempSer->productTempColourExist($product_temp_id,$colour_id,1);
        if(!$product_temp_colour_exist){
            return redirect()->back()->with('error_message', '找不到对应数据');
        }
        $product_temp_colour=$this->productTempSer->getProductTempColourByProdTempIdAndColourId($product_temp_id,$colour_id);
        if(!empty($product_temp_colour)){
            $product_temp_colour->colour_name=ColourTool::getColourNameByColourId($colour_id);
        }
        $temp_custom_zone_positions=$this->productTempSer->getProductTempCustomZonePositions($product_temp->product_temp_category_id);
        return view('backstage.productTemp.addProductTempCustomZone')
            ->with('temp_colour',$product_temp_colour)
            ->with('custom_zone_positions',$temp_custom_zone_positions)
            ->with('product_temp',$product_temp);
    }


    /**
     * 添加创作区域
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postAddCustomZone(Request $request){
        $v=array(
            'product_temp_id'=>'required|string|',
            'colour_code'=>'required|string|max:7|min:7',
            'colour_id'=>'required|integer|min:1',
            'position_type'=>'required|integer|min:1',
            'custom_w_and_screen_w_ratio'=>'required|numeric|between:0,1',
            'custom_aspect_ratio'=>'required|numeric|numeric|between:0,1',
            'image'=>'required|image'
        );
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_code=trim($request->input('colour_code'));
        $image=$request->file('image');
        $colour_id=intval($request->input('colour_id'));
        $position_type=intval($request->input('position_type'));
        $custom_w_and_screen_w_ratio=floatval($request->input('custom_w_and_screen_w_ratio'));
        $custom_aspect_ratio=floatval($request->input('custom_aspect_ratio'));
        $val_data['product_temp_id']=$product_temp_id;
        $val_data['background_colour_code']=$colour_code;
        $val_data['image']=$image;
        $val_data['colour_id']=$colour_id;
        $val_data['position_type']=$position_type;
        $val_data['position_name']=ProductTempTool::getPositionNameByPositionId($position_type);
        $val_data['custom_width_and_screen_width_ratio']=$custom_w_and_screen_w_ratio;
        $val_data['custom_the_area_aspect_ratio']=$custom_aspect_ratio;
        $validator=$this->productTempSer->store($request,$v);
        $validator->after(function($validator) use ($val_data)
        {
            $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($val_data['product_temp_id']);
            if(!$product_temp_exist){
                $validator->errors()->add('product_temp_id', '模板不存在');
            }
            $product_temp_colour_exist=$this->productTempSer->productTempColourExist($val_data['product_temp_id'],$val_data['colour_id'],1);
            if(!$product_temp_colour_exist){
                $validator->errors()->add('colour_id', '该模板当前颜色不存在');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }
        $res=$this->productTempSer->addCustomZone($val_data,1);
        if($res){
            $redirect_url='/backstage/product-temp/custom-zone-list-page?colour_id='.$colour_id.'&product_temp_id='.$product_temp_id;
            return redirect($redirect_url)->with('success_message', '保存成功');
        }
        return redirect()->back()->with('error_message', '保存失败');
    }

    /**
     * 更新定制区域
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateCustomZone(Request $request){
        $v=array(
            'product_temp_id'=>'required|string|',
            'colour_code'=>'required|string|max:7|min:7',
            'colour_id'=>'required|integer|min:1',
            'position_type'=>'required|integer|min:1',
            'image'=>'image|',
            'custom_w_and_screen_w_ratio'=>'required|numeric|between:0,1',
            'custom_aspect_ratio'=>'required|numeric|numeric|between:0,1',

        );
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        $colour_code=trim($request->input('colour_code'));
        $image=$request->file('image');
        $colour_id=intval($request->input('colour_id'));
        $position_type=intval($request->input('position_type'));
        $custom_w_and_screen_w_ratio=floatval($request->input('custom_w_and_screen_w_ratio'));
        $custom_aspect_ratio=floatval($request->input('custom_aspect_ratio'));
        $val_data['product_temp_id']=$product_temp_id;
        $val_data['background_colour_code']=$colour_code;
        $val_data['image']=$image;
        $val_data['colour_id']=$colour_id;
        $val_data['position_type']=$position_type;
        $val_data['position_name']=ProductTempTool::getPositionNameByPositionId($position_type);
        $val_data['custom_width_and_screen_width_ratio']=$custom_w_and_screen_w_ratio;
        $val_data['custom_the_area_aspect_ratio']=$custom_aspect_ratio;
        $val_data['position_name']=ProductTempTool::getPositionNameByPositionId($position_type);
        $validator=$this->productTempSer->store($request,$v);
        $validator->after(function($validator) use ($val_data)
        {
            $product_temp_exist=ProductTempTool::productTempExistsByProdTempId($val_data['product_temp_id']);
            if(!$product_temp_exist){
                $validator->errors()->add('product_temp_id', '模板不存在');
            }
            $product_temp_colour_exist=$this->productTempSer->productTempColourExist($val_data['product_temp_id'],$val_data['colour_id'],1);
            if(!$product_temp_colour_exist){
                $validator->errors()->add('colour_id', '该模板当前颜色不存在');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }
        $res=$this->productTempSer->addCustomZone($val_data,2);
        if($res){
            $redirect_url='/backstage/product-temp/custom-zone-list-page?colour_id='.$colour_id.'&product_temp_id='.$product_temp_id;
            return redirect($redirect_url)->with('success_message', '保存成功');
        }
        return redirect()->back()->with('error_message', '保存失败');
    }
    /**
     * 更新模板定价
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatePrice(Request $request){
        $product_temp_colour_id=intval($request->input('id'));
        $price=floatval($request->input('price'));
        if($price===null){
            $result['state']=-2;
            $result['message']='定价为空';
            return response()->json($result);
        }
        $temp_colour=ProductTempTool::getProductTempColourById($product_temp_colour_id,1);
        if(empty($temp_colour)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $pre_update_price=ProductTempTool::getProdTempPrice($temp_colour->product_temp_id,$temp_colour->colour_id,1);
        //模板单价修改，需要定时去修改，不能直接生效
//        $update_result=$this->productTempSer->productTempColourUpdatePrice($product_temp_colour_id,$price);
        $product_temp_colour=$this->productTempSer->getProductTempColourByProductTempColourId($product_temp_colour_id,1);
        if(!empty($product_temp_colour_id)){
            if($pre_update_price!==$price){
                $input['resource_type']=2;
                $input['resource_id']=$product_temp_colour->product_temp_id;
                $input['colour_id']=$product_temp_colour->colour_id;
                $input['modify_price']=$price;
                $input['price']=$pre_update_price;//更新价格前的价格
                $input['admin_id']=Auth::user()->id;
                $input['admin_email']=Auth::user()->email;
                @$update_price_recoed_result=$this->modifyResourcePriceRecordsSer->save_modify_resource_price_record($input);
            }
        }
        $result['state']=1;
        $result['message']='更新成功';
        return response()->json($result);
    }

    /**
     * 修改产品模板颜色排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateProdTempColourSort(Request $request){
        $product_temp_colour_id=intval($request->input('id'));
        $sort=intval($request->input('sort'));
        if(empty($sort)||!is_int($sort)){
            $result['state']=-2;
            $result['message']='排序数字格式错误';
            return response()->json($result);
        }
        $temp_colour_exist=$this->productTempSer->productTempColourExistById($product_temp_colour_id);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->productTempColourUpdateSort($product_temp_colour_id,$sort);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    /***
     * 更新标签模板绑定，排序规则
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateTagTempMapSort(Request $request){
        $id=intval($request->input('id'));
        $sort=intval($request->input('sort'));
        if(empty($sort)||!is_int($sort)){
            $result['state']=-2;
            $result['message']='排序数字格式错误';
            return response()->json($result);
        }
        $exist=TagTool::tagResourceMapExistById($id,1);
        if(!$exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->tagSer->updateTagResourceMapSort($id,$sort);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     * 更新定制区域工艺对应颜色排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCraftsmanshipColourSort(Request $request){
        $id=intval($request->input('id'));
        $sort=intval($request->input('sort'));
        if(empty($sort)||!is_int($sort)){
            $result['state']=-2;
            $result['message']='排序数字格式错误';
            return response()->json($result);
        }
        $temp_colour_exist=$this->productTempSer->productTempColourExistById($id);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->updateCraftsmanshipColourSort($id,$sort);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    public function updateProductTempColourSort(Request $request){
        $id=intval($request->input('id'));
        $sort=intval($request->input('sort'));
        if(empty($sort)||!is_int($sort)){
            $result['state']=-2;
            $result['message']='排序数字格式错误';
            return response()->json($result);
        }
        $temp_colour_exist=$this->productTempSer->productTempColourExistById($id);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->updateCraftsmanshipColourSort($id,$sort);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    /**
     * 更新产品模板尺码详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateProductTempSizeValues(Request $request){
        $id=intval($request->input('id'));
        $size_values=trim($request->input('size_values'));
        if(empty($id)||!is_int($id)){
            $result['state']=-2;
            $result['message']='id错误';
            return response()->json($result);
        }
        $product_temp_size=ProductTempTool::getProdTempSizeById($id,1);
        if(empty($product_temp_size)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $size_arr=explode('|',$size_values);
        $size_key_num=$this->productTempSer->getProductTempSizeFormatSizeKeyNum($product_temp_size->product_temp_id);
        if(empty($size_key_num)){
            $result['state']=-4;
            $result['message']='请添加尺码格式';
            return response()->json($result);
        }
        $size_arr=array_filter($size_arr);
        if($size_key_num!=sizeof($size_arr)){
            $result['state']=-5;
            $result['message']='尺码详情格式不正确';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->updateProductTempSizeValues($id,$size_values);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    /**
     * 删除颜色下的某一款颜色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delProdTempColour(Request $request){
        $product_temp_colour_id=intval($request->input('id'));
        $temp_colour_exist=$this->productTempSer->productTempColourExistById($product_temp_colour_id);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->delProductTempColourById($product_temp_colour_id);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     * 删除模板中某一尺寸
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delProdTempSize(Request $request){
        $product_temp_size_id=intval($request->input('id'));
        $temp_size_exist=$this->productTempSer->productTempSizeExistById($product_temp_size_id,1);
        if(!$temp_size_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        //删除尺码
        $update_result=$this->productTempSer->delProductTempSizeById($product_temp_size_id);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    /***
     * 删除定制区域工艺对应颜色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteCraftsmanshipColour(Request $request){
        $craftsmanship_colour_id=intval($request->input('id'));
        $temp_colour_exist=ProductTempTool::productTempCraftsmanshipColourExistById($craftsmanship_colour_id,1);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $delete_result=$this->productTempSer->deleteCraftsmanshipColour($craftsmanship_colour_id);
        if($delete_result){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }
    /**
     * 删除定制区域
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCustomZone(Request $request){
        $custom_zone_id=intval($request->input('custom_zone_id'));
        $custom_zone=$this->productTempSer->getProductTempCustomZoneById($custom_zone_id);
        if(empty($custom_zone)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->delCustomZone($custom_zone_id);
        if($update_result){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /**
     * 删除定制区域工艺
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delTempCraftsmanship(Request $request){
        $id=intval($request->input('id'));
        $craftsmanship=ProductTempTool::getCraftsmanshipById($id);
        if(empty($craftsmanship)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->delTempCraftsmanship($id);
        if($update_result){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }

    /**
     * 删除模板
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delProductTemp(Request $request){
        $id=intval($request->input('id'));
        if(empty($id)){
            $result['state']=-4;
            $result['message']='参数错误';
            return response()->json($result);
        }
        $product_temp=$this->productTempSer->getProdTempById($id);
        if(empty($product_temp)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $update_result=$this->productTempSer->delTempCraftsmanship($id);
        if($update_result){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }
    /**
     * 获取模板和颜色下的库存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function prodTempStockByColour(Request $request){
        $product_temp_colour_id=intval($request->input('id'));
        $product_temp_id=trim($request->input('product_temp_id'));
        $temp_colour_exist=$this->productTempSer->productTempColourExistById($product_temp_colour_id,1);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $temp_stocks=$this->productTempSer->prodTempStockByColour($product_temp_id,$product_temp_colour_id);
        if(empty($temp_stocks)){
            $result['state']=-1;
            $result['message']='数据为空';
        }else{
            $result['state']=1;
            $result['message']='成功';
            $result['data']=empty($temp_stocks)?array():$temp_stocks;
        }
        return response()->json($result);
    }

    /**
     * 库存保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveProductTempStock(Request $request){
        $temp_stock_id=intval($request->input('temp_stock_id'));
        $stock=intval($request->input('stock'));
        $colour_id=intval($request->input('colour_id'));
        $temp_id=trim($request->input('temp_id'));
        $size_name=trim($request->input('size_name'));
        $res=$this->productTempSer->saveProductTempStock($stock,$size_name,$colour_id,$temp_id);
        if($res){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     * 保存产品模板实际库存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveProductTempActualStock(Request $request){
        $temp_stock_id=intval($request->input('temp_stock_id'));
        $actual_stock=intval($request->input('actual_stock'));
        $colour_id=intval($request->input('colour_id'));
        $temp_id=trim($request->input('temp_id'));
        $size_name=trim($request->input('size_name'));
        $res=$this->productTempSer->saveProductTempActualStock($actual_stock,$size_name,$colour_id,$temp_id);
        if($res){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }
    /**
     * 产品模板Sku保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveProductTempSku(Request $request){
        $rule=array(
            'temp_stock_id'=>'required|integer|min:1',
            'sku'=>'required|string|alpha_num|max:13|min:13',
        );
        $temp_stock_id=intval($request->input('temp_stock_id'));
        $colour_id=intval($request->input('colour_id'));
        $temp_id=trim($request->input('temp_id'));
        $size_name=trim($request->input('size_name'));
        $sku=trim($request->input('sku'));
        $validator=ProductTempTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('temp_stock_id')){
                $result['state']=-2;
                $result['message']='编号格式不正确';
            }
            if($validator->errors()->first('sku')){
                $result['state']=-3;
                $result['message']='请输入13位sku';
            }
            if(!empty($result)){
                return response()->json($result);
            }
        }
        $old_sku=ProductTempTool::getProductTempSkuById($temp_stock_id);
        if($old_sku===null){
            $result['state']=-5;
            $result['message']='数据不存在';
            return response()->json($result);
        }elseif (!empty($old_sku)){
            $result['state']=-6;
            $result['message']='sku不能修改';
            return response()->json($result);
        }
        $sku_exist=ProductTempTool::productTempSkuExistNotCurrentId($temp_stock_id,$sku,1);
        if($sku_exist){
            $result['state']=-4;
            $result['message']='Sku已使用';
            return response()->json($result);
        }
        $res=$this->productTempSer->saveProductTempSku($sku,$size_name,$colour_id,$temp_id);
        if($res){
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /**
     * 添加产品模板标签页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAddProductTempTag(Request $request)
    {
        return view('backstage.productTemp.addProductTempTag');
    }

    /**
     * 产品模板标签列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function productTempTagList(Request $request)
    {
        $rule=array(
            'page'=>'required|integer|min:1',
            'per_page'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$rule);
        $page=intval($request->input('page'));
        $per_page=intval($request->input('per_page'));
        $redirect_url='/backstage/product-temp/product-temp-tag-list';
        if ($validator->fails())
        {
            if($validator->errors()->first('page')){
                if(!empty($page)){
                    return redirect($redirect_url)->with('error_message', '获取页数有问题!');
                }
            }
            if($validator->errors()->first('per_page')){
                $per_page=10;
            }
        }
        if($page>1000000){
            return redirect($redirect_url)->with('error_message', '获取页数有问题!');
        }
        $data['per_page']=$per_page;
        $tags=$this->productTempSer->getProductTempTagList($data);
        if(empty($tags)){
            return redirect()->back()->with('error_message', '暂无数据!');
        }
        return view('backstage.productTemp.productTempTagList')->with('tags',$tags);
    }

    /**
     * 更新产品模板标签页
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateProductTempPage(Request $request)
    {
        $rule=array(
            'id'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$rule);
        $id=intval($request->input('id'));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                return redirect()->back()->with('error_message', '添加失败!');
            }
        }
        $data['tag_id']=$id;
        $data['app_id']=2;
        $tag=TagTool::getTagByTagId($data);
        if(empty($tag)){
            return redirect()->back()->with('error_message', '暂无数据!');
        }
        $has_binding_tag=TagTool::hasBindingTagByTagId($id,1);
        $p_tag_objs=array();
        if($has_binding_tag){
            $p_tags=TagTool::getTagIdsBySubTagId($id,1);
            foreach ($p_tags as $p_tag){
                $tag_name=TagTool::getTagNameByTagId($p_tag,1);
                if(!empty($tag_name)){
                    $p_tag_arr=array();
                    $p_tag_arr['tag_name']=$tag_name;
                    $p_tag_arr['tag_id']=$p_tag;
                    $p_tag_objs[]=(object)$p_tag_arr;
                }
            }
        }
        $tag->original_image=ImgTool::getImgUrlByImgPath($tag->original_image);
        $already_bound_tag_ids=TagTool::getTagIdsBySubTagId($id,1);
        $already_bound_tag_ids[]=$id;
        $un_bound_tags=$this->tagSer->searchResourceTagList('',2,1,$already_bound_tag_ids);
        $tag->has_sub_tag=$p_tag_objs;
        $tag->p_tags=$p_tag_objs;
        return view('backstage.productTemp.updateProductTempTag')
            ->with('tag',$tag)
            ->with('un_bound_tags',$un_bound_tags);
    }

    /**
     * 批量更新产品模板 多资源标签绑定模板
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bachUpdateProductTemp(Request $request)
    {
        $rule=array(
            'type'=>'required|integer|min:1',
            'type_value'=>'required|integer|min:1',
            'data'=>'required|array|min:1',
        );
        $validator=ProductTempTool::store($request,$rule);
        $type=intval($request->input('type'));
        $type_value=intval($request->input('type_value'));
        $data_arr=$request->input('data');
        if ($validator->fails())
        {
            if($validator->errors()->first('type')){
                $result['message']='类型格式错误';
            }
            if($validator->errors()->first('type_value')){
                $result['message']='类型值格式错误';
            }
            if($validator->errors()->first('data')){
                $result['message']='选中值数组格式错误';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        $data['type']=$type;
        $data['type_value']=$type_value;
        $data['data_arr']=$data_arr;
        $res=$this->productTempSer->bachUpdateProductTemp($data);
        if($res){
            $result['message']='更新成功';
            $result['state']=1;
        }else{
            $result['message']='更新失败';
            $result['state']=-1;
        }
        return response()->json($result);
    }

    /**
     * 获取产品模板资源标签
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getProductTempResourceTags(Request $request)
    {
        $resource_tags=array();
        $resource_tag_ids=TagTool::getResourceTagIds(1,2);//(状态,app_id)
        foreach ($resource_tag_ids as $resource_tag_id){
            $tag_arr=array();
            $tag_arr['tag_id']=$resource_tag_id;
            $tag_arr['tag_name']=TagTool::getTagNameByTagId($resource_tag_id,1);
            $resource_tags[]=(object)$tag_arr;
        }
        $result['state']=-1;
        $result['message']='成功';
        $result['data']=$resource_tags;
        return response()->json($result);
    }

    /**
     * 更新产品模板排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postUpdateProductTempSort(Request $request)
    {
        $v=array(
            'id'=>'required|string|min:22|max:22',
            'sort'=>'required|integer|max:9999'
        );
        $validator=ProductTempTool::store($request,$v);
        $product_temp_id= trim($request->input("id"));
        $sort= intval(trim($request->input("sort")));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result["state"] = -2;
                $result["message"] = "编号不正确";
                return response()->json($result);
            }
            if($validator->errors()->first('sort')){
                $result["state"] = -3;
                $result["message"] = "排序数字格式不正确";
                return response()->json($result);
            }
        }
        $product_temp_exist = ProductTempTool::productTempExistsByProdTempId($product_temp_id);
        if(!$product_temp_exist){
            $result["state"] = -3;
            $result["message"] = "找不到数据";
            return response()->json($result);
        }
        $res =  $this->productTempSer->updateProductTempSort($product_temp_id,$sort);
        if($res){
            $result["state"] = 1;
            $result["message"] = "更新成功";
            return response()->json($result);
        }else{
            $result["state"] = -1;
            $result["message"] = "更新失败";
            return response()->json($result);
        }
    }

    /***
     * 更新自定义区域排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCustomZoneSort(Request $request)
    {
        $v=array(
            'id'=>'required|integer|min:0|',
            'sort'=>'required|integer|max:9999'
        );
        $validator=ProductTempTool::store($request,$v);
        $id= intval($request->input("id"));
        $sort= intval(trim($request->input("sort")));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result["state"] = -2;
                $result["message"] = "编号不正确";
                return response()->json($result);
            }
            if($validator->errors()->first('sort')){
                $result["state"] = -3;
                $result["message"] = "排序数字格式不正确";
                return response()->json($result);
            }
        }
        $res =  $this->productTempSer->updateCustomZoneSort($id,$sort);
        if($res){
            $result["state"] = 1;
            $result["message"] = "更新成功";
            return response()->json($result);
        }else{
            $result["state"] = -1;
            $result["message"] = "更新失败";
            return response()->json($result);
        }
    }

    /***
     * 更新定制区域排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCraftsmanshipSort(Request $request)
    {
        $v=array(
            'id'=>'required|integer|min:0|',
            'sort'=>'required|integer|max:9999'
        );
        $validator=ProductTempTool::store($request,$v);
        $id= intval($request->input("id"));
        $sort= intval(trim($request->input("sort")));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result["state"] = -2;
                $result["message"] = "编号不正确";
                return response()->json($result);
            }
            if($validator->errors()->first('sort')){
                $result["state"] = -3;
                $result["message"] = "排序数字格式不正确";
                return response()->json($result);
            }
        }
        $res =  $this->productTempSer->updateCraftsmanshipSort($id,$sort);
        if($res){
            $result["state"] = 1;
            $result["message"] = "更新成功";
            return response()->json($result);
        }else{
            $result["state"] = -1;
            $result["message"] = "更新失败";
            return response()->json($result);
        }
    }
    /**
     * 获取标签下的产品模板列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getTagTempMaps(Request $request){
        $v=array(
            'id'=>'required|integer|min:1',
        );
        $validator=ProductTempTool::store($request,$v);
        $id= intval($request->input("id"));
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                return redirect()->back()->with('error_message', '系统繁忙!');
            }
        }
        $data['tag_id']=$id;
        $data['app_id']=2;
        $tag=TagTool::getTagByTagId($data);
        if(empty($tag)){
            return redirect()->back()->with('error_message', '标签不存在!');
        }
        $tag->tag_name=TagTool::getTagNameByTagId($id,1);
        $product_temps=$this->productTempSer->getProductTempByTagId($id);
        foreach ($product_temps as $product_temp){
            $temp=$this->productTempSer->getProdTempByProdTempId($product_temp->product_temp_id);
            $product_temp->product_temp_name='';
            $product_temp->category_name='';
            $product_temp->brand_name='';
            if(!empty($temp)){
                $product_temp->product_temp_name=$temp->name;
                $product_temp->category_name=$temp->category_name;
                $product_temp->brand_name=$temp->brand_name;
            }
        }
        $already_binding_product_temp_ids=TagTool::getResourceIdsByTagId($id,2,1);
        $un_sel_temps=$this->productTempTool->getProductTempsByProductTempIds($already_binding_product_temp_ids);
        return view('backstage.productTemp.tagProductTempMaps')
            ->with('tag',$tag)
            ->with('un_sel_product_temps',$un_sel_temps)
            ->with('product_temps',$product_temps);
    }

    /**
     * id删除标签产品模板绑定
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteTagTempMap(Request $request){
        $tag_resource_map_id=intval($request->input('id'));
        $tag_resource_map_exist=TagTool::tagResourceMapIdExistById($tag_resource_map_id);
        if(!$tag_resource_map_exist){
            $result['state']=-2;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $res=$this->productTempSer->deleteTagTempMap($tag_resource_map_id);
        if($res){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }

    /**
     * 添加产品模板资源绑定
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addTagTempMap(Request $request){
        $rule=array(
            'tag_id'=>'required|integer|min:1',
            'resource_id'=>'required|string|min:1',
        );
        $tag_id=intval($request->input('tag_id'));
        $resource_id=trim($request->input('resource_id'));
        $validator=ProductTempTool::store($request,$rule);
        $data['tag_id']=$tag_id;
        $data['resource_id']=$resource_id;
        if ($validator->fails())
        {
            if($validator->errors()->first('tag_id')){
                $result['state']=-2;
                $result['message']=' 标签编号格式错误';
            }
            if($validator->errors()->first('resource_id')){
                $result['state']=-4;
                $result['message']='产品模板编号格式错误';
            }
            if(!empty($result)){
                return response()->json($result);
            }
        }
        $tag_data['tag_id']=$tag_id;
        $tag_data['app_id']=2;
        $tag=TagTool::getTagByTagId($tag_data);
        if(empty($tag)){
            $result['state']=-5;
            $result['message']='标签不存在';
            return response()->json($result);
        }elseif ($tag->tag_type==1){
            $result['state']=-6;
            $result['message']='当前标签绑定范围是全部';
            return response()->json($result);
        }
        $exist=TagTool::resourceIdHasBindingTagId($tag_id,2,$resource_id,1);
        if($exist){
            $result['state']=1;
            $result['message']='保存成功';
            return response()->json($result);
        }
        $res=$this->productTempSer->addTagTempMap($data);
        if($res){
            $result['state']=1;
            $result['message']='保存成功';
        }else{
            $result['state']=-1;
            $result['message']='保存失败';
        }
        return response()->json($result);
    }

    /***
     * 产品模板属性列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getTempAttrList(Request $request)
    {
        $page=intval($request->input('page'));
        $category_id=intval($request->input('category_id'));
        $category_id=empty($category_id)?0:$category_id;
        if(!empty($page)&&(!is_int($page)||($page>100))){
            return redirect()->back()->with('error_message','暂无数据');
        }
        $temp_attributes=$this->productTempSer->getProductTempAttrs($category_id);
        foreach ($temp_attributes as $temp_attribute){
            $temp_attribute->category_name=ProdTempCategoryTool::productTempCategoryNameByProdTempCategoryId($temp_attribute->category_id);
        }
        return view('backstage.productTemp.productTempAttrList')->with('temp_attributes',$temp_attributes);
    }

    /***
     * 添加产品模板属性页
     * @param Request $request
     * @return $this
     */
    public function getAddTempAttrPage(Request $request)
    {
        $temp_categorys=ProdTempCategoryTool::getProdTempCategoryList();
        return view('backstage.productTemp.addProductTempAttr')->with('temp_categorys',$temp_categorys);
    }
    public function getUpdateTempAttrPage(Request $request)
    {
        $rule=array(
            'id'=>'required|integer|min:1',
        );
        $id=intval($request->input('id'));
        $validator=ProductTempTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                return redirect()->back()->with('error_message','编号格式不正确！');
            }
        }
        $temp_attr_exist=ProductTempTool::productTempAttrExistsById($id);
        if(!$temp_attr_exist){
            return redirect()->back()->with('error_message','数据不存在！');
        }
        $temp_attribute=ProductTempTool::getProductTempAttById($id);
        $temp_categorys=ProdTempCategoryTool::getProdTempCategoryList();
        return view('backstage.productTemp.productTempAttrUpdate')
            ->with('temp_categorys',$temp_categorys)
            ->with('temp_attr',$temp_attribute);
    }
    /***
     * 添加产品模板属性
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function addTempAttr(Request $request){
        $rule=array(
            'attr_name'=>'required|string|',
            'category_id'=>'required|integer|min:1',
            'attr_values'=>'required|string|',
        );
        $attr_name=trim($request->input('attr_name'));
        $attr_values=trim($request->input('attr_values'));
        $category_id=intval($request->input('category_id'));
        $val_data['attr_name']=$attr_name;
        $val_data['attr_values']=$attr_values;
        $val_data['category_id']=$category_id;
        $validator=ProductTempTool::store($request,$rule);
        $validator->after(function($validator) use ($val_data)
        {
            $category_exist=ProdTempCategoryTool::productTempCategoryExistsByProdTempCategoryId($val_data['category_id']);
            if(!$category_exist){
                $validator->errors()->add('category_id', '类别不存在');
            }
            $attr_name_exist=ProductTempAttributeService::tempAttNameExistsByCategoryIdAndName(0,$val_data['category_id'],$val_data['attr_name'],1);
            if($attr_name_exist){
                $validator->errors()->add('attr_name', '已存在');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }
        $save_res=$this->productTempSer->addTempAttr($val_data);
        if($save_res){
            return redirect('/backstage/product-temp/temp-attr-list')->with('success_message','保存成功！');
        }
        return redirect()->back()->with('error_message','保存失败！');
    }

    /****
     * 更新产品模板属性
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateTempAttr(Request $request){
        $rule=array(
            'attr_name'=>'required|string|',
            'category_id'=>'required|integer|min:1',
            'attr_id'=>'required|integer|min:1',
            'attr_values'=>'required|string|',
        );
        $attr_name=trim($request->input('attr_name'));
        $attr_values=trim($request->input('attr_values'));
        $category_id=intval($request->input('category_id'));
        $attr_id=intval($request->input('attr_id'));
        $val_data['attr_name']=$attr_name;
        $val_data['attr_values']=$attr_values;
        $val_data['category_id']=$category_id;
        $val_data['attr_id']=$attr_id;
        $validator=ProductTempTool::store($request,$rule);
        $validator->after(function($validator) use ($val_data)
        {
            $category_exist=ProdTempCategoryTool::productTempCategoryExistsByProdTempCategoryId($val_data['category_id']);
            if(!$category_exist){
                $validator->errors()->add('category_id', '类别不存在');
            }
            $attr_name_exist=ProductTempAttributeService::tempAttNameExistsByCategoryIdAndName($val_data['attr_id'],$val_data['category_id'],$val_data['attr_name'],1);
            if($attr_name_exist){
                $validator->errors()->add('attr_name', '已存在');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            Log::info($validator->errors());
            return redirect()->back()->withErrors($validator->errors())->withInput();
        }
        $update_res=$this->productTempSer->updateTempAttr($val_data);
        if($update_res){
            return redirect()->back()->with('success_message','更新成功！');
        }
        return redirect()->back()->with('error_message','更新失败！');
    }

    /****
     * 删除产品模板属性
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delTempAttr(Request $request)
    {
        $rule=array(
            'id'=>'required|integer|min:1',
        );
        $attr_id=intval($request->input('id'));
        $validator=ProductTempTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result['state']=-2;
                $result['message']='id格式不正确';
                return response()->json($result);
            }
        }
        $temp_attr_exist=ProductTempTool::productTempAttrExistsById($attr_id);
        if(!$temp_attr_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $res=$this->productTempSer->deleteProductTempAttr($attr_id);
        if($res){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }

    /***
     * 更新模板属性类型值
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateTempAttrValues(Request $request)
    {
        $rule=array(
            'id'=>'required|integer|min:1',
            'attr_values'=>'required|string',
        );
        $attr_id=intval($request->input('id'));
        $attr_values=trim($request->input('attr_values'));
        $validator=ProductTempTool::store($request,$rule);
        if ($validator->fails())
        {
            if($validator->errors()->first('id')){
                $result['state']=-2;
                $result['message']='id格式不正确';
                return response()->json($result);
            }
        }
        $temp_attr_exist=ProductTempTool::productTempAttrExistsById($attr_id);
        if(!$temp_attr_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $res=$this->productTempSer->updateTempAttrValues($attr_id,$attr_values);
        if($res){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }

    /***
     * 产品库存excel导出页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function stockExportPage(Request $request){
        $page=intval($request->input('page'));
        $brand_id=trim($request->input('brand_id'));
        $search=trim($request->input('search',''));
        $temp_category_id=intval($request->input('category_id',0));
        if(!empty($page)&&(!is_int($page)||($page>100))){
            return redirect()->back()->with('error_message','暂无数据');
        }
        $product_temps=$this->productTempSer->searchProdTempSimpleList($search,$brand_id,$temp_category_id);
        $brands=BrandToolService::getBrandsWhereNotInBrandIds(array());
        $product_temp_categorys=ProdTempCategoryTool::getProdTempCategoryList();
        return view('backstage.productTemp.stock_export')
            ->with('brands',$brands)
            ->with('brand_id',$brand_id)
            ->with('product_temps',$product_temps)
            ->with('temp_category_id',$temp_category_id)
            ->with('temp_categorys',$product_temp_categorys);
    }

    /***
     * excel导出产品模板库存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function stockExport(Request $request){
        $temp_ids=$request->input('temp_ids');
        if (!is_array($temp_ids)||empty($temp_ids)){
            $result['state']=-1;
            $result['message']='暂无数据';
            return response()->json($result);
        }
        $res=$this->productTempSer->exportStockExcelByTempIds($temp_ids);
        if(empty($res)){
            $result['state']=-1;
            $result['message']='生成失败';
        }else{
            $result['state']=1;
            $result['message']='成功！';
            $result['excel_url']=$res;
        }
        return response()->json($result);
    }

    /***
     * 产品模板库存批量修改
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function stockImportPage(Request $request){
        return view('backstage.productTemp.stock_import');
    }

    /***
     * 导入excel更新库存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function stockImport(Request $request){
        $excel=$request->file('excel');
        $data['excel']=$excel;
        $res=$this->productTempSer->stockImport($data);
        if(empty($res)){
            return redirect()->back()->with('error_message','更新失败');
        }
        if($res['state']==1){
            return redirect()->back()->with('success_message','更新成功');
        }
        return redirect()->back()->with('error_message',$res['message']);
    }

    /***
     * 删除制作工艺下默认颜色列表下颜色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteCraftsmanshipDefaultColour(Request $request){
        $colour_id=intval($request->input('colour_id'));
        $temp_colour_exist=$this->productTempTool->craftsmanshipDefaultColourIdExistByColourId($colour_id,1);
        if(!$temp_colour_exist){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $delete_result=$this->productTempSer->deleteCraftsmanshipDefaultColour($colour_id);
        if($delete_result){
            $result['state']=1;
            $result['message']='删除成功';
        }else{
            $result['state']=-1;
            $result['message']='删除失败';
        }
        return response()->json($result);
    }
    public function addCraftsmanshipDefaultColour(Request $request){
        $colour_id=intval($request->input('colour_id'));
        if(empty($colour_id)||!is_int($colour_id)){
            $result['state']=-2;
            $result['message']='参数格式不正确';
            return response()->json($result);
        }
        $colour_exist=ColourTool::colourExistByColourId($colour_id);
        if(!$colour_exist){
            $result['state']=-3;
            $result['message']='该颜色不存在';
            return response()->json($result);
        }
        $data['colour_id']=$colour_id;
        $res=$this->productTempSer->addCraftsmanshipDefaultColour($data);
        if($res){
            $result['state']=1;
            $result['message']='添加成功';
        }else{
            $result['state']=-1;
            $result['message']='添加失败';
        }
        return response()->json($result);
    }
}
